package com.qianfeng.smartdevices.controller;

import com.github.pagehelper.PageInfo;
import com.qianfeng.smartdevices.annotations.AopLogAnnotation;
import com.qianfeng.smartdevices.dto.R;
import com.qianfeng.smartdevices.pojo.Areas;
import com.qianfeng.smartdevices.service.AreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/areas")
@Api(tags = "区域管理模块")
public class AreaController {

    private AreaService areaService;

    @Autowired
    public void setAreaService(AreaService areaService) {
        this.areaService = areaService;
    }

    /**
     * 查询对应状态的所有的父区域
     *
     * @param stauts 状态,可以为空
     * @return
     */
    @GetMapping("/areas/parent")
    @ApiOperation("查询区域")
    @AopLogAnnotation("区域查询")
    public R findParentAreaByStatus(Long stauts) {
        List<Areas> areasList = areaService.findParentAreaByStatus(stauts);
        return R.setOK(areasList);
    }


    @PostMapping("/area")
    @ApiOperation("添加区域")
    @AopLogAnnotation("添加区域")
    public R addArea(@RequestBody Areas areas) throws Exception {
        areaService.addArea(areas);
        return R.setOK();
    }
    @GetMapping("/areas")
    @AopLogAnnotation("区域分页查询")
    public R findAllAreasByNameLikeAndStatusEquals(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10")int limit,String areaname, Long status,Long parentid) {
        PageInfo<Areas> pageInfo = areaService.findAllAreasByNameLikeAndStatusEquals(page, limit, areaname, status,parentid);
        return R.setOK(pageInfo);
    }

    @PutMapping("/area")
    @AopLogAnnotation("区域更新")
    public R updateArea(@RequestBody Areas areas) {
        areaService.updateArea(areas);
        return R.setOK();
    }

    @DeleteMapping("/areas")
    @AopLogAnnotation("区域删除")
    public R deleteAreaById(Long[] ids){
        areaService.deleteAreaById(ids);
        return R.setOK();
    }
    @GetMapping("/area/parent/{id}")
    public R getAreaByParentId( @PathVariable Long id) {
        List<Areas> areaList = areaService.getAreaByParentId(id);
        return R.setOK(areaList);
    }
    @GetMapping("/area/{id}")
    @ApiOperation("根据id 获取区域数据")
    public R getAreasById(@PathVariable Long id) {
        Areas areaById = areaService.getAreaById(id);
        return R.setOK(areaById);

    }
}
